<?php
/**
 * @Created on 2021/05/25
 * @Author Qi
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Customer extends Base{

    //客户管理
    public function index(){

        $where = array();
        $name=request()->param('name');
        if($name){
            $where['name'] = array('like',"%{$name}%");
        }
        $company=request()->param('company');
        if($company){
            $where['company'] = array('like',"%{$company}%");
        }
        $tel=request()->param('tel');
        if($tel){
            $where['tel'] = array('like',"%{$tel}%");
        }
        $cname=request()->param('cname');
        if($cname){
            $where['conact_name'] = array('like',"%{$cname}%");
        }
        $start=request()->param('start');
        if($start){
            $where['open_time'] = array('egt',$start);
        }
        $end=request()->param('end');
        if($end){
            $where['open_time'] = array('elt',$end);
        }
        if(!empty($start) && !empty($end)){
            $where['open_time'] = array('between time',["{$start}","{$end}"]);
        }
        $list=db('customer')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('name',$name);
        $this->assign('company',$company);
        $this->assign('tel',$tel);
        $this->assign('cname',$cname);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch();
    }

    //添加
    public function add(){

        if(request()->isPost()){
            $name = trim(request()->param('name'));
            $company = trim(request()->param('company'));
            $tel = trim(request()->param('tel'));
            $conact_name = trim(request()->param('conact_name'));
            $src = trim(request()->param('src'));
            $bank_name = trim(request()->param('bank_name'));
            $open_bank = trim(request()->param('open_bank'));
            $number = trim(request()->param('number'));
            $open_time = trim(request()->param('open_time'));
            $data['name'] = $name;
            $data['company'] = $company;
            $data['tel'] = $tel;
            $data['conact_name'] = $conact_name;
            $data['src'] = $src;
            $data['bank_name'] = $bank_name;
            $data['open_bank'] = $open_bank;
            $data['number'] = $number;
            $data['open_time'] = $open_time;
            $data['add_time'] = time();
            $true=db('customer')->insert($data);
            if($true){
                system_log('customer','添加客户');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            return $this->fetch();
        }
    }

    //修改
    public function edit(){
        if(request()->isPost()){
            $id = request()->param('id');
            $name = trim(request()->param('name'));
            $company = trim(request()->param('company'));
            $tel = trim(request()->param('tel'));
            $conact_name = trim(request()->param('conact_name'));
            $src = trim(request()->param('src'));
            $bank_name = trim(request()->param('bank_name'));
            $open_bank = trim(request()->param('open_bank'));
            $number = trim(request()->param('number'));
            $open_time = trim(request()->param('open_time'));
            $data['name'] = $name;
            $data['company'] = $company;
            $data['tel'] = $tel;
            $data['conact_name'] = $conact_name;
            $data['src'] = $src;
            $data['bank_name'] = $bank_name;
            $data['open_bank'] = $open_bank;
            $data['number'] = $number;
            $data['open_time'] = $open_time;
            $true=db('customer')->where('id',$id)->update($data);
            if($true){
                system_log('customer','修改客户');
                return json(['status'=>1,'message'=>'修改成功']);
            }else{
                return json(['status'=>0,'message'=>'修改失败']);
            }
        }else{
            $id=request()->param('id');
            $info=db('customer')->where('id',$id)->find();
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        $id=request()->param('id');
        $true=db('customer')->where('id',$id)->delete();
        if($true){
            system_log('customer','删除客户');
            return json(['status'=>1,'message'=>'删除成功']);
        }else{
            return json(['status'=>0,'message'=>'删除失败']);
        }
    }

}